package com.lz.football.module.mail.view
{
	import com.common.stdlib.ArrayUtil;
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.GoodsType;
	import com.liangzhong.model.type.OtherGoodType;
	import com.lz.football.module.mail.IMailView;
	import com.lz.football.module.mail.item.MailItem;
	
	import common.base.GoodsUsePanel;
	import common.comp.TabButtonBar;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.LButton;
	import lz.controls.PageComponent;
	import lz.controls.core.TitleWindow;
	import lz.controls.events.PageEvent;

	/**
	 *邮箱
	 * @author lake
	 * 
	 */	
	public class MailView extends TitleWindow implements IMailView
	{
		/**
		 *选项卡 
		 */		
		private var buttonBar :TabButtonBar;
		/**
		 *分页 
		 */		
		private var pageComp 		:PageComponent;
		/**
		 *显示内容列表 
		 */		
		private var showDataList	:Array = [];
		/**
		 *条目列表 
		 */		
		private var itemList		:Array = [];
		/**
		 *内容容器 
		 */		
		private var container 		:MovieClip;
		
		public function MailView()
		{
			super();
		}
		/**
		 *初始化 
		 * @param view
		 * 
		 */		
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.module.mail.PanelSkin");
			this.addChild(_symbolSkin);
			//初始化宽高
			width = _symbolSkin.width;
			height = _symbolSkin.height;
			//初始化UI界面控件
			windowBg = _symbolSkin.windowBg;//背景框
			container = _symbolSkin.container;//邮件显示条目容器
			//分页初始化
			pageComp = new PageComponent();
			pageComp.initUI(_symbolSkin.pageComp);
			//设置可拖动
			dragEnabled = true;
			//设置关闭按钮
			setCloseBtn(_symbolSkin.closeBtn);
			//设置坐标不自适应
			resizeEnabled = false;
			//选项bar 初始化
			buttonBar = new TabButtonBar();
			var i :int = 0;
			var lbtn :LButton;
			var lbtns :Array = [];
			for(;i<=4;i++)
			{
				lbtn = new LButton(_symbolSkin["buttonBar"]["skin_" + i]);
				lbtn.enable = true;
				lbtns.push(lbtn);
			}
			buttonBar.setButtons(lbtns);
			buttonBar.selectTabButton(lbtns[0]);
			//初始化条目
			i = 0;
			var mailItem :MailItem;
			for(;i<6;i++)
			{
				mailItem = new MailItem();
				mailItem.initUI();
				mailItem.y = i * 60;
				itemList.push(mailItem);
			}
			//添加事件
			addEvtListener();
			//初始数据 全部
			showDataList = AppData.getInstance().mailDataManager.mailList;
			//分页设置
			pageComp.totalNumbers = showDataList.length;
			pageComp.pageSize = 6;
			pageComp.curPage = 1;
			//浅入
			fadeIn();
		}
		/**
		 *添加事件 
		 * 
		 */		
		public function addEvtListener():void
		{
			pageComp.addEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			buttonBar.addEventListener(Event.CHANGE,onTabChangeHandler);
			AppData.getInstance().mailDataManager.addEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 *页数改变 
		 * @param e
		 * 
		 */		
		private function pageChangeHandler(e :PageEvent):void
		{
			//显示数据
			setItemPageDatas(ArrayUtil.getPageSizeDatas(pageComp.curPage,pageComp.pageSize,showDataList));
		}
		/**
		 *设置数据 
		 * @param datas
		 * 
		 */		
		public function setItemPageDatas(datas :Array):void
		{
			DisplayObjectUtil.removeAllChildren(container);
			var item :MailItem; 
			var i :int = 0;
			for each(var itemObj :* in datas)
			{
				if(i>=6)return;
				item = itemList[i];
				item.data = itemObj;
				container.addChild(item);
				i ++;
			}
		}
		/**
		 *选项卡改变 
		 * @param e
		 * 
		 */		
		private function onTabChangeHandler(e :Event):void
		{
			refreshData();
			pageComp.curPage = 1;
		}
		/**
		 *刷新数据 
		 * 
		 */		
		private function refreshData():void
		{
			var selectType :int = buttonBar.selectType;
			showDataList = AppData.getInstance().mailDataManager.getMailListByType(selectType);
			pageComp.totalNumbers = showDataList.length;
			pageComp.curPage = pageComp.curPage;
		}
		/**
		 *数据改变 
		 * @param e
		 * 
		 */		
		private function dataChangeHandler(e :Event):void
		{
			refreshData();
		}
		/**
		 *删除事件 
		 * 
		 */		
		public function removeEvtListener():void
		{
			pageComp.removeEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			buttonBar.removeEventListener(Event.CHANGE,onTabChangeHandler);
			AppData.getInstance().mailDataManager.removeEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 * 关闭按钮响应
		 * @param e
		 * 
		 */		
		override protected function onCloseHandler(e :MouseEvent):void
		{
			_symbolSkin.mouseEnabled = false;
			_symbolSkin.mouseChildren = false;
			fadeOut();
		}
		/**
		 *淡出完毕 
		 */		
		override protected function fadeOutComplete():void
		{
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE,true));
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
		}
		/**
		 *销毁 
		 * 
		 */		
		public function destroyView():void
		{
			removeEvtListener();
			showDataList = null;
			for each(var item :MailItem in itemList)
			{
				item.destroyView();
			}
			itemList = null;
			buttonBar.destroyView();
			buttonBar = null;
		}
		/**
		 *显示某一标签类型内容 
		 * @param $obj
		 * 
		 */		
		public function showType($obj:Object):void
		{
		}
	}
}